package geekstudy.week4;

import java.util.*;

/**
 *
 * https://leetcode-cn.com/problems/design-twitter/submissions/
 * 355. 设计推特
 *
 * 执行结果：  通过  显示详情
 *
 * 添加备注
 * 执行用时：12 ms, 在所有 Java 提交中击败了44.85% 的用户
 * 内存消耗：36.8 MB, 在所有 Java 提交中击败了19.76% 的用户
 *
 *
 */
public class Twitter {

    private HashMap<Integer, Set<Integer>> followUsers;
    private HashMap<Integer, HashMap<Integer, Integer>> tweets;
    private int time;

    /** Initialize your data structure here. */
    public Twitter() {
        this.time = 0;
        followUsers = new HashMap<>();
        tweets = new HashMap<>();
    }

    /** Compose a new tweet. */
    public void postTweet(int userId, int tweetId) {
        if(tweets.containsKey(userId)){
            tweets.get(userId).put(tweetId, time++);
        }else{
            HashMap<Integer, Integer> temp = new HashMap<>();
            temp.put(tweetId, time++);
            tweets.put(userId, temp);
        }
    }

    /** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
    public List<Integer> getNewsFeed(int userId) {
        HashMap<Integer, Integer> map = new HashMap<>();
        if(followUsers.containsKey(userId)){
            for(Integer s : followUsers.get(userId)){
                if(tweets.containsKey(s)){
                    for(Map.Entry<Integer, Integer> entry : tweets.get(s).entrySet()){
                        map.put(entry.getKey(), entry.getValue());
                    }
                }
            }
        }
        if(tweets.containsKey(userId)){
            for(Map.Entry<Integer, Integer> entry : tweets.get(userId).entrySet()){
                map.put(entry.getKey(), entry.getValue());
            }
        }
        List<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, (o1, o2) -> {
            return o2.getValue() - o1.getValue();
        });
        List<Integer> res = new ArrayList<>();
        int index = 0;
        for(Map.Entry<Integer, Integer> entry : list){
            res.add(entry.getKey());
            index++;
            if(index == 10) return res;
        }
        return res;
    }

    /** Follower follows a followee. If the operation is invalid, it should be a no-op. */
    public void follow(int followerId, int followeeId) {
        if(followerId == followeeId) return;
        if(followUsers.containsKey(followerId)){
            followUsers.get(followerId).add(followeeId);
        }else{
            Set<Integer> temp = new HashSet<>();
            temp.add(followeeId);
            followUsers.put(followerId, temp);
        }
    }

    /** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
    public void unfollow(int followerId, int followeeId) {
        if(followerId == followeeId) return;
        if(followUsers.containsKey(followerId)){
            followUsers.get(followerId).remove(followeeId);
        }else{
            return;
        }
    }
}


/**
 * Your Twitter object will be instantiated and called as such:
 * Twitter obj = new Twitter();
 * obj.postTweet(userId,tweetId);
 * List<Integer> param_2 = obj.getNewsFeed(userId);
 * obj.follow(followerId,followeeId);
 * obj.unfollow(followerId,followeeId);
 */
